/**
 * @author		Sergey Suzdalskiy
 * @e-mail:		sergey.suzdalskiy@gmail.com
 */
package ua.kiev.gcore.logger
{
	import flash.utils.getTimer;

	public class LoggerMessageCollection
	{
		private static var collection:Vector.<LoggerMessage> = new Vector.<LoggerMessage>();
		
		public static function AddMessage(message:LoggerMessage):LoggerMessage
		{
			collection.push(message);
			return message;
		}
		
		public static function AddNewMessage(Message:String, Type:int, Level:int):LoggerMessage
		{
			var Time:Number = getTimer() / 1000;
			var message:LoggerMessage = new LoggerMessage(Message, Type, Time, Level);
			collection.push(message);
			return message;
		}
		
		public static function GetAll():Vector.<LoggerMessage>
		{
			return collection;
		}
		
		public static function GetLastByLevel(Level:int, count:int):Vector.<LoggerMessage>
		{
			var result:Vector.<LoggerMessage> = GetByLevel(Level);
			
			if(count >= result.length){
				return result;
			}			
			
			return result.slice(result.length - count, result.length);
		}
		
		public static function GetByLevel(level:int):Vector.<LoggerMessage>
		{
			var result:Vector.<LoggerMessage> = new Vector.<LoggerMessage>();
			for each(var message:LoggerMessage in collection){
				if((level == Logger.All_LEVEL) || (message.Level == level)){
					result.push(message);
				}
			}
			return result;
		}
		
		public static function GetByType(type:int):Vector.<LoggerMessage>
		{
			var result:Vector.<LoggerMessage> = new Vector.<LoggerMessage>();
			for each(var message:LoggerMessage in collection){
				if(message.Type == type){
					result.push(message);
				}
			}
			return result;
		}
	}
}